﻿/*********************************
作者: UnitySir
创建日期: 2023年10月21日 19:42:34
        功能: Nothing
*********************************/

using CliWrap;
using CliWrap.Buffered;

namespace NetEnvironmentVariable ; 

    public class NodeJsEV
    {
         private static string s_npm = string.Empty;
        /// <summary>
        /// 设置Nodejs环境
        /// </summary>
        /// <param name="rootPath">nodejs安装目录</param>
        public static async Task Set(string rootPath) {
            //1.判断当前目录是否有 node.exe 文件
            var rtPath = Path.GetFullPath(rootPath).Replace(@"\\", @"\");
            string pathExists = Path.Combine(rtPath, "node.exe");
            if (File.Exists(pathExists) == false) {
                Console.WriteLine("请设置正确的Node目录!");
                return;
            }
            
            //2.在安装目录创建 node_cache 和 node_global 目录 并使用 npm配置
           string node_cache = Path.Combine(rtPath, "node_cache");
           string node_global = Path.Combine(rtPath, "node_global");
            if (Directory.Exists(node_cache) == false)
                Directory.CreateDirectory(node_cache);
            if (Directory.Exists(node_global) == false)
                Directory.CreateDirectory(node_global);
            
            //3.在 Path 中 添加 "..\nodejs" 和 "..\nodejs\node_global"
            EV.SetByKey("Path", @$"{rtPath};{node_global}");
            
            //4.npm 配置全局目录
            var npm = Path.Combine(rtPath, "npm.cmd");
            await Cli.Wrap(npm)
                     .WithArguments($"config set cache {node_cache}")
                     .WithValidation(CommandResultValidation.None)
                     .ExecuteBufferedAsync();
            await Cli.Wrap(npm)
                     .WithArguments($"config set prefix {node_global}")
                     .WithValidation(CommandResultValidation.None)
                     .ExecuteBufferedAsync();
            
            Console.WriteLine("Nodejs环境配置完成!");
            
            s_npm = npm;
        }

        public static async Task InitNpm(string source)
        {
            await Cli.Wrap(s_npm)
                .WithArguments($"config set registry {source}")
                .WithValidation(CommandResultValidation.None)
                .ExecuteBufferedAsync();
            await Console.Out.WriteLineAsync("npm镜像源初始化完成!");
        }
        public static async Task InstallVite()
        {
            await Cli.Wrap(s_npm)
                .WithArguments($"install vite -g")
                .WithValidation(CommandResultValidation.None)
                .ExecuteBufferedAsync();
            await Cli.Wrap("vite")
                .WithArguments($"-v")
                .WithValidation(CommandResultValidation.None)
                .ExecuteBufferedAsync();
            await Console.Out.WriteLineAsync("Vite安装完成!");
        }
        
        public static async Task InstallVue3()
        {
            await Cli.Wrap(s_npm)
                .WithArguments($"install -g create-vue")
                .WithValidation(CommandResultValidation.None)
                .ExecuteBufferedAsync();
            await Cli.Wrap(s_npm)
                .WithArguments($"install -g @vue/cli")
                .WithValidation(CommandResultValidation.None)
                .ExecuteBufferedAsync();
            await Cli.Wrap("vue")
                .WithArguments($"-V")
                .WithValidation(CommandResultValidation.None)
                .ExecuteBufferedAsync();
            await Console.Out.WriteLineAsync("Vue安装完成!");
        }

        public static async Task InstallReact()
        {
            await Cli.Wrap(s_npm)
                .WithArguments($"install -g create-react-app")
                .WithValidation(CommandResultValidation.None)
                .ExecuteBufferedAsync();
            await Cli.Wrap(s_npm)
                .WithArguments($"install -g react")
                .WithValidation(CommandResultValidation.None)
                .ExecuteBufferedAsync();
            await Cli.Wrap("create-react-app")
                .WithArguments($"-V")
                .WithValidation(CommandResultValidation.None)
                .ExecuteBufferedAsync();
            await Console.Out.WriteLineAsync("React安装完成!");
        }
    }